﻿namespace ExamV5.OnlineExam
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Wuqi.Webdiyer;

    public class StudyList : Page
    {
        protected Button butExp;
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected DropDownList DDLDept;
        protected DropDownList DDLQuery;
        protected DropDownList DDLSubject;
        protected DropDownList DDLUPDept;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Label labRescordCount;
        protected Label labTip;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBPrevPage;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected TextBox txtQuery;
        protected UpdatePanel UPPart;

        protected AspNetPager aspPager;
        protected void butExp_Click(object sender, EventArgs e)
        {
            try
            {
                string key = Guid.NewGuid().ToString("N").ToUpper();
                string path = base.Server.MapPath("../") + @"\PublicFile\StudyUserData" + key + ".csv";
                string str2 = "";
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                StreamWriter writer = new StreamWriter(path, true, Encoding.GetEncoding("GB2312"));
                writer.WriteLine("准考证号,考生姓名,所属部门,所属职务,所属岗位,练习科目,题库总数,答题总数,完成率,正确率");
                string cmdText = "select d.DeptName,d.UPID,c.UserGWName,c.UserGWLevel,c.Worker,a.UserCode,c.UserName,b.SubjectName,a.SubjectID from StudyHistory a,SubjectInfo b,UserInfo c,DeptInfo d where d.ID=c.DeptID and a.UserCode=c.UserCode and c.Flag='A' and a.SubjectID=b.ID ";
                if (this.DDLDept.SelectedItem.Value != "0")
                {
                    cmdText = cmdText + " and c.DeptID in (select b.ID from DeptInfo b where " + this.publicFun_0.GetManagDeptID(this.publicFun_0.CheckString(this.DDLDept.SelectedItem.Value)) + ")";
                }
                if ((this.DDLSubject.Items.Count != 0) && (this.DDLSubject.SelectedItem.Value != "0"))
                {
                    cmdText = cmdText + " and b.ID=" + this.DDLSubject.SelectedItem.Value;
                }
                if (this.txtQuery.Text.Trim() != "")
                {
                    cmdText = cmdText + " and " + this.DDLQuery.SelectedItem.Value + " like'%" + this.txtQuery.Text.Trim() + "%'";
                }
                if (!(this.ViewState["UserPower"].ToString() == "Y"))
                {
                    cmdText = cmdText + " and b.ID<>0 ";
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('在线监考管理权限未开放！')", true);
                }
                else
                {
                    cmdText = cmdText + " and b.ID<>0 ";
                }
                cmdText = cmdText + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                connection.Open();
                SqlCommand command = new SqlCommand(cmdText, connection);
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    str2 = "";
                    str2 = ("" + str2 + reader["UserCode"].ToString().Trim() + ",") + reader["UserName"].ToString().Trim() + ",";
                    if (reader["UPID"].ToString().Trim() == "0")
                    {
                        str2 = str2 + reader["DeptName"].ToString().Trim() + ",";
                    }
                    else
                    {
                        str2 = str2 + this.publicFun_0.GetValues("select DeptName from DeptInfo where ID=" + reader["UPID"].ToString().Trim(), "DeptName") + @"\" + reader["DeptName"].ToString().Trim() + ",";
                    }
                    str2 = ((str2 + reader["Worker"].ToString().Trim() + ",") + reader["UserGWName"].ToString().Trim() + ",") + reader["SubjectName"].ToString().Trim() + ",";
                    double num4 = Convert.ToDouble(this.publicFun_0.GetValues("select count(*) as CountInfo from RubricInfo where flag='B' and SubjectID=" + reader["SubjectID"].ToString().Trim(), "CountInfo"));
                    str2 = str2 + num4.ToString() + ",";
                    string str3 = this.method_1(reader["UserCode"].ToString().Trim(), reader["SubjectID"].ToString().Trim());
                    double num = 0.0;
                    double num2 = 0.0;
                    double num3 = 0.0;
                    num2 = Convert.ToDouble(str3.Substring(0, str3.IndexOf(",")));
                    num3 = Convert.ToDouble(str3.Substring(str3.IndexOf(",") + 1));
                    num = num2 + num3;
                    str2 = str2 + num.ToString() + ",";
                    if (!(num4 == 0.0))
                    {
                        num = ((num2 + num3) / num4) * 100.0;
                    }
                    else
                    {
                        num = 0.0;
                    }
                    str2 = str2 + num.ToString("0.00") + "%,";
                    if (num3 == 0.0)
                    {
                        if (num2 == 0.0)
                        {
                            num = 0.0;
                        }
                        else
                        {
                            num = 100.0;
                        }
                    }
                    else
                    {
                        num = (num2 / (num2 + num3)) * 100.0;
                    }
                    str2 = str2 + num.ToString("0.00") + "%,";
                    writer.WriteLine(str2);
                }
                reader.Close();
                command.Dispose();
                connection.Close();
                connection.Dispose();
                writer.Flush();
                writer.Close();
                this.labTip.Text = " 导出成功！单击【<a href='../FrmDownLoad.aspx?FileName=StudyUserData" + key + ".csv' target=_blank><font color=red>下载</font></a>】进行保存";
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BExpErrTip", @"alert('导出数据失败！\n\n请将PublicFile目录设置为Everyone用户可修改权限！')", true);
            }
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.aspPager.CurrentPageIndex = 1;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.DataItemIndex == -1)
            //{
            //    this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
            //    this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            //}
            //else
            //{
            //    e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
            //    if ((e.Row.DataItemIndex % 2) == 0)
            //    {
            //        e.Row.Attributes.Add("bgcolor", "#FFFFFF");
            //        e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
            //    }
            //    else
            //    {
            //        e.Row.Attributes.Add("bgcolor", "#FFFFFF");
            //        e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
            //    }
            //}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label label3 = (Label)e.Row.Cells[3].FindControl("labRubricCount");
                Label label4 = (Label)e.Row.Cells[4].FindControl("labStudyCount");
                Label label2 = (Label)e.Row.Cells[5].FindControl("labCompStatus");
                Label label = (Label)e.Row.Cells[6].FindControl("labTrueStatus");
                double num4 = Convert.ToDouble(this.publicFun_0.GetValues("select count(*) as CountInfo from RubricInfo where flag='B' and SubjectID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["SubjectID"].ToString(), "CountInfo"));
                label3.Text = num4.ToString();
                string str2 = this.method_1(e.Row.Cells[0].Text.Trim(), this.DataGridInfo.DataKeys[e.Row.RowIndex]["SubjectID"].ToString());
                double num = 0.0;
                double num2 = 0.0;
                double num3 = 0.0;
                num2 = Convert.ToDouble(str2.Substring(0, str2.IndexOf(",")));
                num3 = Convert.ToDouble(str2.Substring(str2.IndexOf(",") + 1));
                num = num2 + num3;
                label4.Text = num.ToString();
                if (num4 == 0.0)
                {
                    num = 0.0;
                }
                else
                {
                    num = ((num2 + num3) / num4) * 100.0;
                }
                label2.Text = num.ToString("0.00") + "%";
                if (num3 == 0.0)
                {
                    if (num2 == 0.0)
                    {
                        num = 0.0;
                    }
                    else
                    {
                        num = 100.0;
                    }
                }
                else
                {
                    num = (num2 / (num2 + num3)) * 100.0;
                }
                label.Text = num.ToString("0.00") + "%";
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (!(this.ViewState["SortType"].ToString() == " Desc"))
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "UserCode":
                            e.Row.Cells[0].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "UserName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "SubjectName":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↓"));
                            return;
                    }
                    e.Row.Cells[0].Controls.Add(new LiteralControl("↓"));
                }
                else
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "UserCode":
                            e.Row.Cells[0].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "UserName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "SubjectName":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↑"));
                            return;
                    }
                    e.Row.Cells[0].Controls.Add(new LiteralControl("↑"));
                }
            }
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (!(this.ViewState["SortFiled"].ToString() == sortExpression))
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            else if (!(this.ViewState["SortType"].ToString() == " Desc"))
            {
                this.ViewState["SortType"] = " Desc";
            }
            else
            {
                this.ViewState["SortType"] = " ASC";
            }
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DataGridInfo_PreRender(object sender, EventArgs e)
        {
            // 使用<TH>替换<TD> 
            this.DataGridInfo.UseAccessibleHeader = true;
            //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹
            this.DataGridInfo.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

        protected void aspPager_PageChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = this.aspPager.CurrentPageIndex - 1;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DDLDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DDLSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void DDLUPDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DDLDept.Items.Clear();
            this.publicFun_0.LoadDeptInfo(this.DDLDept, this.DDLUPDept.SelectedItem.Value, this.httpCookie_0.Value, "B");
            if (this.DDLDept.Items.Count == 0)
            {
                if (!(this.DDLUPDept.SelectedItem.Value == "0"))
                {
                    ListItem item = new ListItem(this.DDLUPDept.SelectedItem.Text, this.DDLUPDept.SelectedItem.Value);
                    this.DDLDept.Items.Add(item);
                }
                else
                {
                    ListItem item2 = new ListItem("--所有部门--", "0");
                    this.DDLDept.Items.Insert(0, item2);
                }
            }
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        private void method_0()
        {
            string selectCommandText = "select a.UserCode,c.UserName,b.SubjectName,a.SubjectID from StudyHistory a,SubjectInfo b,UserInfo c where a.UserCode=c.UserCode and c.Flag='A' and a.SubjectID=b.ID ";
            if (this.DDLDept.SelectedItem.Value != "0")
            {
                selectCommandText = selectCommandText + " and c.DeptID in (select b.ID from DeptInfo b where " + this.publicFun_0.GetManagDeptID(this.publicFun_0.CheckString(this.DDLDept.SelectedItem.Value)) + ")";
            }
            if ((this.DDLSubject.Items.Count != 0) && (this.DDLSubject.SelectedItem.Value != "0"))
            {
                selectCommandText = selectCommandText + " and b.ID=" + this.DDLSubject.SelectedItem.Value;
            }
            if (this.txtQuery.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and " + this.DDLQuery.SelectedItem.Value + " like'%" + this.txtQuery.Text.Trim() + "%'";
            }
            if (this.ViewState["UserPower"].ToString() == "Y")
            {
                selectCommandText = selectCommandText + " and b.ID<>0 ";
            }
            else
            {
                selectCommandText = selectCommandText + " and b.ID<>0 ";
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('在线监考管理权限未开放！')", true);
            }
            selectCommandText = selectCommandText + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            //this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int)(this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            this.labRescordCount.Text = "总共&nbsp;<i>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</i>&nbsp;条记录&nbsp;<b>" + Convert.ToString((int)(this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b>&nbsp;页&nbsp;";
            this.aspPager.RecordCount = dataSet.Tables["DataInfo"].Rows.Count;
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        private string method_1(string string_0, string string_1)
        {
            string str = "0,0";
            int num = 0;
            string str2 = "";
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("select RubricCountYS,RubricCountYD,RubricCountYB,RubricCountYF,RubricCountYC,RubricCountYA,RubricCountYT,RubricCountYAn,RubricCountYJ,RubricCountNS,RubricCountND,RubricCountNB,RubricCountNF,RubricCountNC,RubricCountNA,RubricCountNT,RubricCountNAn,RubricCountNJ from StudyHistory where UserCode='" + string_0 + "' and SubjectID=" + string_1 + " ", connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (!reader.Read())
            {
                str = "0,0";
            }
            else
            {
                str2 = reader["RubricCountYS"].ToString() + reader["RubricCountYD"].ToString() + reader["RubricCountYB"].ToString() + reader["RubricCountYF"].ToString() + reader["RubricCountYC"].ToString() + reader["RubricCountYA"].ToString() + reader["RubricCountYT"].ToString() + reader["RubricCountYAn"].ToString() + reader["RubricCountYJ"].ToString();
                num = Convert.ToInt32(this.publicFun_0.GetValues("select  Count(*) as countInfo from RubricInfo where ID in(" + str2 + "0)", "countInfo"));
                str2 = reader["RubricCountNS"].ToString() + reader["RubricCountND"].ToString() + reader["RubricCountNB"].ToString() + reader["RubricCountNF"].ToString() + reader["RubricCountNC"].ToString() + reader["RubricCountNA"].ToString() + reader["RubricCountNT"].ToString() + reader["RubricCountNAn"].ToString() + reader["RubricCountNJ"].ToString();
                str = num.ToString() + "," + Convert.ToInt32(this.publicFun_0.GetValues("select  Count(*) as countInfo from RubricInfo where ID in(" + str2 + "0)", "countInfo")).ToString();
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            return str;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() != "Yes")
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请联系管理员！')", true);
                    }
                    else
                    {
                        this.DDLUPDept.DataTextField = "DeptName";
                        this.DDLUPDept.DataValueField = "ID";
                        if (this.httpCookie_2.Value == "C")
                        {
                            this.DDLUPDept.DataSource = this.publicFun_0.RetrunDataView("select ID,DeptName from DeptInfo where UPID=0  order by SortInfo,ID");
                        }
                        else
                        {
                            this.DDLUPDept.DataSource = this.publicFun_0.RetrunDataView("select ID,DeptName from DeptInfo where ID=" + this.httpCookie_3.Value + " order by SortInfo,ID");
                        }
                        this.DDLUPDept.DataBind();
                        if (this.httpCookie_2.Value == "C")
                        {
                            this.DDLUPDept.SelectedIndex = -1;
                            this.DDLDept.SelectedIndex = -1;
                            ListItem item2 = new ListItem("--所有单位--", "0");
                            ListItem item3 = new ListItem("--所有部门--", "0");
                            this.DDLDept.Items.Insert(0, item3);
                            this.DDLUPDept.Items.Insert(0, item2);
                        }
                        else
                        {
                            this.DDLDept.Items.Clear();
                            this.publicFun_0.LoadDeptInfo(this.DDLDept, this.DDLUPDept.SelectedItem.Value, this.httpCookie_0.Value, "B");
                            if (this.DDLDept.Items.Count == 0)
                            {
                                ListItem item = new ListItem(this.DDLUPDept.SelectedItem.Text, this.DDLUPDept.SelectedItem.Value);
                                this.DDLDept.Items.Add(item);
                            }
                        }
                        this.txtQuery.Attributes.Add("onkeydown", "SubmitKeyClick('butQuery');");
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddExam,ManagExam  from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read() && ((reader["AddExam"].ToString() == "Y") || (reader["ManagExam"].ToString() == "Y")))
                        {
                            this.ViewState["UserPower"] = "Y";
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString;
                        this.ViewState["SortFiled"] = "UserCode";
                        this.ViewState["SortType"] = " Desc";
                        this.publicFun_0.LoadSubjectInfo(this.DDLSubject, this.httpCookie_0.Value, "N", "B", this.httpCookie_3.Value);
                        this.DDLSubject.Items.Insert(0, new ListItem("--所有科目信息--", "0"));
                        this.method_0();
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

